const { sequelize, Industry } = require('../models');
const { Op } = require('sequelize');

class IndustryService {
	static injector(ctn) {
		return [
			ctn.make('varal.app')
		]
	}
	constructor(app) {
		this.app = app;
	}

	async get(filter, page = 1, page_size = 20) {
		const { name } = filter;

		const where = {};
		if (name) where['name'] = { [Op.like]: `%${name}%` };

		let category1 = [];
		let category2 = [];
		let category3 = [];
		let category4 = [];

		if (Object.keys(where).length) {
			//获取所有符合条件的记录
			const data = await Industry.findAll({
				where
			});

			if (!data.length) return {
				data: [],
				total: 0
			}

			category1 = Array.from(new Set(data.map((d) => d.category1)));
			category2 = Array.from(new Set(data.map((d) => d.category2)));
			category3 = Array.from(new Set(data.map((d) => d.category3)));
			category4 = Array.from(new Set(data.map((d) => d.category4)));
		}

		const { count: total, rows } = await Industry.findAndCountAll({
			attributes: ['id'],
			where: (() => {
				const where = { parent_id: 0 };
				if (category1.length) where.category1 = { [Op.in]: category1 };

				return where;
			})(),

			offset: (page - 1) * page_size,
			limit: page_size * 1,
			order: [
				['id', 'asc']
			]
		});

		const data = await Industry.findAll({
			attributes: ['id', 'name', 'category1', 'category2', 'category3', 'category4', ['id', 'key']],
			where: {
				'id': { [Op.in]: rows.map((d) => d.id) }
			},
			include: {
				model: Industry,
				as: 'children',
				attributes: ['id', 'name', 'category1', 'category2', 'category3', 'category4', ['id', 'key']],

				where: (() => {
					const where = {};
					if (category2.length) where.category2 = { [Op.in]: category2 };
					return where;
				})(),

				include: {
					model: Industry,
					as: 'children',
					attributes: ['id', 'name', 'category1', 'category2', 'category3', 'category4', ['id', 'key']],

					where: (() => {
						const where = {};
						if (category3.length) where.category3 = { [Op.in]: category3 };
						return where;
					})(),

					include: {
						model: Industry,
						as: 'children',
						attributes: ['id', 'name', 'category1', 'category2', 'category3', 'category4', ['id', 'key']],

						where: (() => {
							const where = {};
							if (category4.length) where.category4 = { [Op.in]: category4 };
							return where;
						})(),

						required: false
					},

					required: false
				},

				required: false
			},
			order: [
				['id', 'asc']
			]
		})

		return {
			data,
			total
		}
	}

	async cascader(parent_id) {
		return Industry.findAll({
			raw: false,

			attributes: [['id', 'value'], ['name', 'label'], [sequelize.literal(`(case when category4>0 then true else false end)`), 'isLeaf']],
			where: {
				parent_id
			},
			order: [
				['id', 'asc']
			]
		})
	}

	async save(params) {
		['create_time', 'update_time', 'delete_time'].forEach((d) => {
			delete params[d];
		});

		if (params?.id) {
			return Industry.update(params, {
				where: { id: params.id }
			})
		} else {
			//if (!params.parent_id) params.parent_id = 0;

			return Industry.create(params);
		}
	}

	async delete(id) {
		const data = await Industry.findByPk(id);

		Industry.destroy({
			where: { id },

			force: true
		});

		//删除所有子栏目
		if (!data.category2) {
			Industry.destroy({
				where: { category1: data.category1 },
				force: true
			})
		} else if (!data.category3) {
			Industry.destroy({
				where: { category2: data.category2 },
				force: true
			})
		} else if (!data.category4) {
			Industry.destroy({
				where: { category3: data.category3 },
				force: true
			})
		}

		return true;
	}
}

exports = module.exports = IndustryService;
